package pt.isel.pdm.yamba;

import java.io.InputStream;
import java.net.URL;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
public class UserActivity extends Activity implements ServiceConnection{
	private IUserInfoOpers iopers;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.user);
		bindService(new Intent(this, UserInfoPull.class), this, BIND_AUTO_CREATE);
	}
	@Override
	public void onServiceConnected(ComponentName arg0, IBinder arg1) {
		iopers = IUserInfoOpers.Stub.asInterface(arg1);
		Log.d("PDM", "UserActivity onServiceConnected() connected");
		new AsyncTask<Void, Void, Void>(){
			@Override
			protected Void doInBackground(Void... arg0) {
				try{
					Global.name = iopers.getUserName(Global.username, Global.password, Global.apiuri);
					try{
						Global.image = BitmapFactory.decodeStream((InputStream)new URL(iopers.getUserImage(Global.username, Global.password, Global.apiuri)).getContent()); 
					}catch(Exception e){
					}
					Global.statuses = iopers.getUserMessages(Global.username, Global.password, Global.apiuri);
					Global.following = iopers.getUserFollowing(Global.username, Global.password, Global.apiuri);
					Global.followers = iopers.getUserFollowers(Global.username, Global.password, Global.apiuri);
				}catch(Exception e){
					Global.name = null;
				}
				return null;
			}
			@Override
			protected void onPostExecute(Void arg){
				if(Global.name == null){
					Toast.makeText(UserActivity.this, getString(R.string.apiError), Toast.LENGTH_LONG).show();
					return;
				}
				try{
					((TextView) findViewById(R.id.idUserNameText)).setText(Global.name);
					try{
						((ImageView) findViewById(R.id.idUserImage)).setImageBitmap(Global.image); 
					}catch(Exception e){
						Log.d("PDM", "User image error: " + e.getMessage());
					}
					((TextView) findViewById(R.id.idUserNumMsgText)).setText(Global.statuses + "");
					((TextView) findViewById(R.id.idUserNumScsText)).setText(Global.following + "");
					((TextView) findViewById(R.id.idUserNumStsText)).setText(Global.followers + "");
				}catch(Exception e){
					Toast.makeText(UserActivity.this, getString(R.string.apiError), Toast.LENGTH_LONG).show();
				}
			}
		}.execute((Void) null);
	}

	@Override
	public void onServiceDisconnected(ComponentName arg0) {
		iopers = null;
	      Log.d("PDM", "UserActivity onServiceDisconnected() disconnected");
	}
	protected void onResume() {
		super.onResume();
	    Log.d("PDM", "UserActivity onResume()");
	}
	@Override
	protected void onStop() {
		super.onStop();
		if (iopers != null)
			unbindService(this);
	}
}
